<?php

class items_ctrl extends Controller
{


    public function __construct()
    {
        parent::__construct();

    }

    //查询商品
    public function get_items()
    {
        if (!$this->ajax_err) {

            $pageno = (!empty($_REQUEST['pageNo'])) ? (int)($_REQUEST['pageNo']) : 1;
            $pagesize = (!empty($_REQUEST['pageSize'])) ? (int)($_REQUEST['pageSize']) : 40;
            $catsId = $_REQUEST['catsId'];
            $keyword = str_replace("+", "%20", urlencode($_REQUEST['keyWord']));
            //$keyword = addslashes(json_encode($_REQUEST['keyWord'])) == '\"\"'? '': addslashes(json_encode($_REQUEST['keyWord']));

            $_mdl = Loader::mdl('items_mdl');
            $data = $_mdl->get_items($pageno, $pagesize, $catsId, $keyword);

            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }


    //前端触发扫描商品
    public function front_scan()
    {
        if (!$this->ajax_err) {

            $key = 'scan_time';
            //ccDel($key);
            $rs_time = ccLoad($key);
            //如果缓存中存在数据,直接从缓存中读取数据
            if ($rs_time != false) {
                //dump(microtime(true).'----'.$rs_time.'-----'.(microtime(true) - $rs_time));
                if(microtime(true) - $rs_time > 3600){
                    $_SESSION['tb_uid'] = '3170729146';
                    $_SESSION['tb_nick'] = 'MAC魅可官方旗舰店';
                    $_SESSION['top_session'] = '6101c01ad17410a22f10795190678f67ef0fa20a7e083343170729146';
                    $_SESSION['last_name'] = 'MAC魅可官方旗舰店';
                    $_SESSION['timeout'] = '18600';

                    include_once(CTRLS_PATH.'/top_ctrl.php');
                    $scan = new top_ctrl();
                    $scan->scan_all_item();


                    //删除照片
                    $_mdl = Loader::top('wantu');
                    $_mdl->del_dir();
                    $_mdl->create_dir();

                    ccSave($key, microtime(true), 36000);
                }
            }else{
                ccSave($key, microtime(true), 36000);
            }

            dump(ccLoad($key));
            return true;
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }


    //修改商品图
    public function set_item()
    {
        if (!$this->ajax_err && $this->uid) {

            $num_iid = $_REQUEST['num_iid'];
            $item_imgs = $_REQUEST['item_imgs'];

            $_mdl = Loader::mdl('items_mdl');
            $data = $_mdl->set_item($num_iid,$item_imgs);

            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }

    //查询商品 sku级别
    public function get_items_v()
    {
        if (!$this->ajax_err) {

            $pageno = (!empty($_REQUEST['pageNo'])) ? (int)($_REQUEST['pageNo']) : 1;
            $pagesize = (!empty($_REQUEST['pageSize'])) ? (int)($_REQUEST['pageSize']) : 1;
            $cat_ids = $_REQUEST['catIds'];
            $keyword = $_REQUEST['keyWord'];

            $_mdl = Loader::mdl('items_mdl');
            $data = $_mdl->get_items_v($pageno, $pagesize, $cat_ids, $keyword);
            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }



}
